/**
 * Created by ajhin on 4/21/2015.
 */
package societyGames.codeAlong.model.errorChecking
{
import com.societyGames.system.EventObject;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TextEvent;

import societyGames.codeAlong.model.ModulesDocument;

public class TestCaseChecker extends EventDispatcher
{
  private var _statusChangedEventObject:EventObject;
  private var _completedEventObject:EventObject;
  private var _typeName:String;
  private var _solutionChecker:SolutionChecker;
  private var _resultsText:String;
  private var _typeFunctions:Array;
  private var _typeNames:Array;
  private var _typeIndex:int;
  private var _modulesDocument:ModulesDocument;
  private var _startIndex:int;
  private var _endIndex:int;

  public function TestCaseChecker()
  {
    this._statusChangedEventObject = new EventObject(this, "STATUS_CHANGED", TextEvent);
    this._completedEventObject = new EventObject(this, "COMPLETED");
  }

  public function get statusChangedEventObject():EventObject
  {
    return this._statusChangedEventObject;
  }

  public function get completedEventObject():EventObject
  {
    return this._completedEventObject;
  }

  public function get resultsText():String
  {
    return this._resultsText;
  }

  public function test(modulesDocument:ModulesDocument, startIndex:int = 0, endIndex:int = -1):void
  {
    _modulesDocument = modulesDocument;
    _startIndex = startIndex;
    _endIndex = endIndex;

    _solutionChecker = new SolutionChecker();
    _solutionChecker.completedEventObject.addEventListener(onSolutionCheckerCompleted, false, 0, true);
    _solutionChecker.currentModuleIndexChangedEventObject.addEventListener(onTestSolutionCurrentModuleIndexChanged, false, 0, true);

    _resultsText = "";
    _typeFunctions = [
      _solutionChecker.generateSolutionCode,
      _solutionChecker.generateFail,
      _solutionChecker.generateSyntaxError,
      _solutionChecker.generateException
      ];
    _typeNames = [
      "Solution",
      "Fail",
      "Syntax Error",
      "Exception"
      ];
    _typeIndex = 0;

    testNextType();
  }

  private function testNextType():void
  {
    if (_typeIndex == _typeFunctions.length)
    {
      _solutionChecker.completedEventObject.removeEventListener(onSolutionCheckerCompleted);
      _solutionChecker.currentModuleIndexChangedEventObject.removeEventListener(onSolutionCheckerCompleted);
      _solutionChecker = null;
      this._completedEventObject.dispatchEvent();
      return;
    }

    var generateCode:Function = _typeFunctions[_typeIndex];
    _typeName = _typeNames[_typeIndex];
    _typeIndex++;
    _solutionChecker.testSolutions(_modulesDocument.modulesContainer.xml, _modulesDocument.solutionsContainer.xml,
            generateCode, _startIndex, _endIndex);
  }

  private function onTestSolutionCurrentModuleIndexChanged(event:Event):void
  {
    var status:String = this._typeName + " " + _solutionChecker.currentModuleId; //_solutionChecker.currentModuleIndex;
    this._statusChangedEventObject.dispatchEvent(new TextEvent(this._statusChangedEventObject.type, false, false, status));
  }

  private function onSolutionCheckerCompleted(event:Event):void
  {
    _resultsText += "---" + _typeName + "---\n";

    if (_solutionChecker.errors.length == 0)
    {
      _resultsText += "No errors.\n";
    }
    for each (var error:String in _solutionChecker.errors)
    {
      _resultsText += error + "\n";
    }
    testNextType();
  }
}
}
